import { HEADER, MAP_INDEX } from "../Constant"
import { RESTFulDeclare, TYPE } from "../Type"

type RawHeader = `${string}: ${string}`

export function Headers(...value: RawHeader[]) {
  return (target: any, name: string, _: PropertyDescriptor) => {
    let headers: Map<string, Record<string, string>> = target[HEADER]
    if (!headers) {
      headers = new Map<string, Record<string, string>>()
      target[HEADER] = headers
    }
    let header = {}
    value.forEach(item => {
      let [key, value] = item.split(": ")
      header[key] = value
    })
    headers.set(name, header)
  }
}

export function HeaderMap(target: any, propertyKey: string, parameterIndex: number) {
  let types: Map<number, RESTFulDeclare> = target[MAP_INDEX+propertyKey]
  if (!types) {
    types = new Map<number, RESTFulDeclare>()
    target[MAP_INDEX+propertyKey] = types
  }
  types.set(parameterIndex, { type: TYPE.HEADERS, key: '' })
}

export function Header(value: string) {
  return (target: any, propertyKey: string, parameterIndex: number) => {
    let types: Map<number, RESTFulDeclare> = target[MAP_INDEX+propertyKey]
    if (!types) {
      types = new Map<number, RESTFulDeclare>()
      target[MAP_INDEX+propertyKey] = types
    }
    types.set(parameterIndex, { type: TYPE.HEADER, key: value })
  }
}